<template>
  <el-dialog
    :title="subject"
    :visible.sync="showDialog"
    width="450px"
    destroy-on-close
    close-on-press-escape
    @close="handleClear"
  >
    <el-form size="small" label-width="auto" :model="list">
      <el-form-item label="学科名称" prop="subjectName" required>
        <el-input
          placeholder="请输入学科名称"
          v-model="list.subjectName"
        ></el-input>
      </el-form-item>
      <el-form-item label="是否显示">
        <el-switch
          v-model="list.isFrontDisplay"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClear">取 消</el-button>
      <el-button size="small" type="primary" @click="apSub">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addList, putList } from '@/api/hmmm/subjects'
export default {
  name: 'JFCSubjectsAdd',
  props: {
    showDialog: {
      type: Boolean,
      required: true
    },
    list: {
      type: Object,
      required: true
    },
    subject: {
      type: String,
      required: true
    }
  },
  data () {
    return {
    }
  },
  methods: {
    // 取消事件函数
    handleClear () {
      try {
        // 关闭弹层
        this.$emit('update:showDialog', false)
        // 清空input
        this.$emit('update:list', { subjectName: '', isFrontDisplay: 1 })
      } catch (error) {}
    },
    async apSub () {
      if (this.list.id) {
        await putList(this.list)
        await this.$parent.getList()
        this.$message.success('修改成功')
        this.$emit('update:list', { subjectName: '', isFrontDisplay: 1 })
        this.$emit('update:showDialog', false)
      } else {
        await addList(this.list)
        await this.$parent.getList()
        this.$message.success('新增成功')
        this.$emit('update:list', { subjectName: '', isFrontDisplay: 1 })
        this.$emit('update:showDialog', false)
      }
    }
  }
}
</script>

<style scoped>
.smallText {
  font-size: 12px;
}
</style>
